package com.wenwan.manage.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.wenwan.manage.mapper.FeedBackMapper;
import com.wenwan.manage.pojo.FeedBack;
import com.wenwan.manage.pojo.LimitPage;
import com.wenwan.manage.service.FeedBackService;
import com.wenwan.manage.setting.RegExpSetting;
import com.wenwan.manage.setting.StatusSetting;
import com.wenwan.manage.util.Util;

@Service
public class FeedBackServiceImpl implements FeedBackService {

	@Autowired
	private FeedBackMapper feedBackMapper;

	@Override
	public Map<String, Object> deleteById(String id) {
		// TODO Auto-generated method stub
		// DataToObject<Boolean> dto = new DataToObject<Boolean>();
		Map<String, Object> dto = new HashMap<String, Object>();

		if (id != null && !id.matches(RegExpSetting.PARA_ID)) {
			dto.put("data", false);
			dto.put("msg", "id" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("id" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		Map<String, Object> map = Util.initMap();
		map.put("id", id);

		Boolean data = feedBackMapper.deleteById(map);
		// dto.setData(data);
		dto.put("data", data);
		dto.put("msg", StatusSetting.MSG_200);
		dto.put("code", StatusSetting.CODE_200);
		return dto;
	}

	@Override
	public Map<String, Object> selectAll(Integer currentPage, Integer pageSize) {
		// TODO Auto-generated method stub
		// DataToObject<List<FeedBack>> dto = new DataToObject<List<FeedBack>>();
		Map<String, Object> dto = new HashMap<String, Object>();

		if (currentPage != null && currentPage < 1) {
			dto.put("data", false);
			dto.put("msg", "currentPage" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ONE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setMsg("currentPage" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ONE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (pageSize != null && pageSize < 1) {
			dto.put("data", false);
			dto.put("msg", "pageSize" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ONE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setMsg("pageSize" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ONE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		Map<String, Object> map = Util.initMap();
		map.put("currentPage", (currentPage - 1) * pageSize);
		map.put("pageSize", pageSize);

		List<FeedBack> data = feedBackMapper.selectAll(map);

		LimitPage limitPage = new LimitPage();
		limitPage.setCurrentPage(currentPage);
		limitPage.setPageSize(pageSize);
		Integer maxSize = feedBackMapper.selectMaxSize(map);
		limitPage.setMaxSize(maxSize);

		// dto.setLimitPage(limitPage);
		// dto.setData(data);
		dto.put("data", data);
		dto.put("limitPage", limitPage);
		dto.put("msg", "pageSize" + StatusSetting.MSG_10050_IS_CAN_NOT_LESS_ONE);
		dto.put("code", StatusSetting.CODE_10050);
		return dto;
	}

	@Override
	public Map<String, Object> selectOneById(String id) {
		// TODO Auto-generated method stub
		// DataToObject<FeedBack> dto = new DataToObject<FeedBack>();
		Map<String, Object> dto = new HashMap<String, Object>();

		if (id != null && !id.matches(RegExpSetting.PARA_ID)) {
			dto.put("data", false);
			dto.put("msg", "id" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setMsg("id" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		Map<String, Object> map = Util.initMap();
		map.put("id", id);

		FeedBack data = feedBackMapper.selectOneById(map);
		// dto.setData(data);
		dto.put("data", data);
		dto.put("msg", StatusSetting.MSG_200);
		dto.put("code", StatusSetting.CODE_200);
		return dto;
	}

	@Override
	public Map<String, Object> create(String phone, String content, String images) {
		// TODO Auto-generated method stub
		// DataToObject<Boolean> dto = new DataToObject<Boolean>();
		Map<String, Object> dto = new HashMap<String, Object>();

		if (phone != null && !phone.matches(RegExpSetting.PHONE)) {
			dto.put("data", false);
			dto.put("msg", "phone" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("phone" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		if (content != null && content.length() < 20) {
			dto.put("data", false);
			dto.put("msg", "content" + StatusSetting.MSG_10050_LENGTH_IS_CAN_NOT_LESS_FIFTEEN_WORD);
			dto.put("code", StatusSetting.CODE_10050);
			// dto.setData(false);
			// dto.setMsg("content" +
			// StatusSetting.MSG_10050_LENGTH_IS_CAN_NOT_LESS_FIFTEEN_WORD);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		try {

			new Gson().fromJson(images, new TypeToken<List<String>>() {
			}.getType());

		} catch (Exception e) {
			dto.put("data", false);
			dto.put("msg", "images" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			dto.put("code", StatusSetting.CODE_10050);
			// TODO: handle exception
			// dto.setData(false);
			// dto.setMsg("images" + StatusSetting.MSG_10050_FORMAT_IS_MISTAKE);
			// dto.setCode(StatusSetting.CODE_10050);
			return dto;
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", Util.UUID());
		map.put("createTime", Util.getTime());
		map.put("updateTime", Util.getTime());
		map.put("phone", phone);

		map.put("content", content);
		map.put("images", images);

		Boolean data = feedBackMapper.create(map);
		// dto.setData(data);
		dto.put("data", data);
		dto.put("msg", StatusSetting.MSG_200);
		dto.put("code", StatusSetting.CODE_200);
		return dto;
	}

}
